package com.ist.acceleratedrawdemo

import android.content.Context
import android.content.SharedPreferences

class SpManager private constructor(context: Context) {
    private val mSharedPreferences: SharedPreferences

    init {
        mSharedPreferences = context.getSharedPreferences(FILE_NAME, 0)
    }

    fun getInt(s: String?, n: Int): Int {
        return mSharedPreferences.getInt(s, n)
    }

    fun putInt(s: String?, n: Int) {
        mSharedPreferences.edit().putInt(s, n).commit()
    }

    fun getString(s: String?, def: String?): String? {
        return mSharedPreferences.getString(s, def)
    }

    fun putString(key: String?, `val`: String?) {
        mSharedPreferences.edit().putString(key, `val`).commit()
    }

    fun getBoolean(s: String?, def: Boolean): Boolean {
        return mSharedPreferences.getBoolean(s, def)
    }

    fun putBoolean(key: String?, `val`: Boolean) {
        mSharedPreferences.edit().putBoolean(key, `val`).commit()
    }

    fun getFloat(s: String?, def: Float): Float {
        return mSharedPreferences.getFloat(s, def)
    }

    fun putFloat(key: String?, `val`: Float) {
        mSharedPreferences.edit().putFloat(key, `val`).commit()
    }

    companion object {
        private var sInstance: SpManager? = null
        private const val FILE_NAME = "IstDrawDemo"
        const val KEY_STROKE_WIDTH = "stroke_width"
        const val KEY_STROKE_COLOR = "stroke_color"
        const val KEY_ERASER_WIDTH = "eraser_color"
        const val KEY_BLUR_ENABLE = "stroke_blur_enbale"
        const val KEY_BLUR_SIZE = "stroke_blur_size"
        const val KEY_BLUR_TYPE = "stroke_blur_type"
        @JvmStatic
        val instance: SpManager?
            get() {
                synchronized(SpManager::class.java) { checkNotNull(sInstance) { SpManager::class.java.getSimpleName() + " is not initialized, call initializeInstance(..) method first." } }
                return sInstance
            }

        fun initializeInstance(context: Context) {
            synchronized(SpManager::class.java) {
                if (sInstance == null) {
                    sInstance = SpManager(context)
                }
            }
        }
    }
}
